package com.ruoyi.mk.controller;

import java.io.*;
import java.lang.reflect.Type;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ruoyi.common.annotation.Anonymous;
import org.springframework.core.io.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mk.domain.MkCategory;
import com.ruoyi.mk.service.IMkCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 南疆东线品类查询Controller
 * 
 * @author ruoyi
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/tool/category")
public class MkCategoryController extends BaseController
{
    @Autowired
    private IMkCategoryService mkCategoryService;

    /**
     * 查询南疆东线品类查询列表
     */
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(MkCategory mkCategory)
    {
        startPage();
        List<MkCategory> list = mkCategoryService.selectMkCategoryList(mkCategory);
        return getDataTable(list);
    }

    /**
     * 查询南疆东线品类查询列表
     */
    @GetMapping("/listAll")
    @Anonymous
    public AjaxResult listAll(MkCategory mkCategory) {
        List<MkCategory> list = mkCategoryService.selectMkCategoryList(mkCategory);
        return success(list);
    }

    /**
     * 导出南疆东线品类查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:category:export')")
    @Log(title = "南疆东线品类查询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MkCategory mkCategory)
    {
        List<MkCategory> list = mkCategoryService.selectMkCategoryList(mkCategory);
        ExcelUtil<MkCategory> util = new ExcelUtil<MkCategory>(MkCategory.class);
        util.exportExcel(response, list, "南疆东线品类查询数据");
    }

    /**
     * 获取南疆东线品类查询详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mkCategoryService.selectMkCategoryById(id));
    }

    /**
     * 新增南疆东线品类查询
     */
    @PreAuthorize("@ss.hasPermi('system:category:add')")
    @Log(title = "南疆东线品类查询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MkCategory mkCategory)
    {
        return toAjax(mkCategoryService.insertMkCategory(mkCategory));
    }

    /**
     * 修改南疆东线品类查询
     */
    @PreAuthorize("@ss.hasPermi('system:category:edit')")
    @Log(title = "南疆东线品类查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MkCategory mkCategory)
    {
        return toAjax(mkCategoryService.updateMkCategory(mkCategory));
    }

    /**
     * 删除南疆东线品类查询
     */
    @PreAuthorize("@ss.hasPermi('system:category:remove')")
    @Log(title = "南疆东线品类查询", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mkCategoryService.deleteMkCategoryByIds(ids));
    }

    /**
     * 下载导入模版
     * */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<MkCategory> util = new ExcelUtil<>(MkCategory.class);
        util.importTemplateExcel(response,"南疆东线表");
    }

    /**
     * 导入南疆东线
     */
    @PreAuthorize("@ss.hasPermi('system:category:import')")
    @Log(title = "南疆东线导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws IOException {
        ExcelUtil<MkCategory> util = new ExcelUtil<>(MkCategory.class);
        List<MkCategory> list = util.importExcel(file.getInputStream());
        for (MkCategory mkCategory : list) {
            mkCategoryService.insertMkCategory(mkCategory);
        }
        return AjaxResult.success();
    }

    /**
     * 解析json文件数据插入数据库
     */
    @PreAuthorize("@ss.hasPermi('system:category:insertJson')")
    @Log(title = "解析json文件数据插入数据库", businessType = BusinessType.IMPORT)
    @PostMapping("/insertJson")
    public AjaxResult insertJson(MultipartFile file) throws IOException {
        String flieName = file.getOriginalFilename();
        String fileType = flieName.substring(flieName.lastIndexOf(".") + 1);
        if ("json".equals(fileType)) {
            try {
                File filePath = new File("D:\\shop.json");
                BufferedReader br = new BufferedReader(new FileReader(filePath));
                String line;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                String jsonContent = sb.toString();
                logger.info("读取的Json文件内容为：" + jsonContent);
                Gson gson = new Gson();
                Type type = new TypeToken<List<MkCategory>>() {}.getType();
                List<MkCategory> list = gson.fromJson(jsonContent, type);
                for (MkCategory mkCategory : list) {
                    mkCategoryService.insertMkCategory(mkCategory);
                }
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            AjaxResult.success("文件格式错误！");
        }
        return AjaxResult.success();
    }

    @Log(title = "记录用户浏览次数")
    @PostMapping("/getViewsCount")
    public AjaxResult getViewsCount() {
        Long count = mkCategoryService.getViewsCount();
        return AjaxResult.success(count);
    }

}
